<?php
	include_once('./global.php');
	session_start();
	
	$url = $_SERVER['REQUEST_URI'];
	$url_arr = parse_url($url);
	parse_str($url_arr['query'],$url_arr);
	$article_id = $url_arr[id];
	
	$sql_select = "select * from articleinfo where A_AID = '$article_id'";
	$query_select = $mysqli->query($sql_select);
	$result_select = $query_select->fetch_assoc();
	
	if(isset($_POST['update_news'])){
		$statue='1';
		if($_POST['article_overhead'] == 0){
			$pushid = '0';
			$pushpid = '0';
		}else if($_POST['article_overhead'] != 0){
			$pushid = $_POST['article_overhead'];
			if(is_uploaded_file($_FILES['uploaded']['tmp_name'])){
				$uploaded = $_FILES["uploaded"];
				if($uploaded['error'] > 0){
					echo "<script>alert('上传出错，请重试')</script>";
				}else{
					if($uploaded["size"] <= '3145728'){
						$date = mktime();	
						$name = $_POST['uploaded_name'];
						$type = $_FILES['uploaded']['type'];
						$image_name = $name.$date;
						$path = "../images/";
						$file_pathname = "../images/".$name.".png";				
						if(move_uploaded_file($_FILES['uploaded']['tmp_name'],$file_pathname)){
							$sql_insert_pic = "INSERT INTO pictureinfo(`P_PID`, `P_Name`, `P_ID`, `P_Path`, `P_Class`, `P_Adid`, `P_Adclass`, `P_Date`, `P_Url`) VALUES (NULL,'$name','$article_id','$path','2','2','$pushid','$date',NULL)";
							/*echo $sql_insert_pic;*/
							$query_insert_pic = $mysqli->query($sql_insert_pic);
							if($query_insert_pic){
								$sql_select_pid = "select P_PID from pictureinfo where P_Date = '$date'";
								$query_select_pid = $mysqli->query($sql_select_pid);
								$result_select_pid = $query_select_pid->fetch_assoc();
								if($result_select_pid){
									$pushpid = $result_select_pid['P_PID'];
								}else{
									echo "<script>alert('图片查询失败')</script>";
								}
							}else{
								echo "<script>alert('图片添加失败')</script>";
								echo $mysqli->error;
							}
						}else{
							echo "失败";
						}				
					}else{
						echo "<script>alert('图片太大')</script>";
					}
				}
			}else{
				echo "<script>alert('请选择你要上传的图片')</script>";
			}
			$sql_update_push = "update pushinfo set P_Pushappid = NULL,P_Pushartid='$article_id',P_Pushpid = '$pushpid' where P_Pushid = '$pushid' ";
/*			echo $sql_update_push;*/
			$query_update_push = $mysqli->query($sql_update_push);
			if($query_update_push){
				echo "<script>alert('push添加更新')</script>";
				$sql_select_push = "select P_Pushid,P_Pushpid from pushinfo where P_Pushartid = '$article_id'";
				$query_select_push = $mysqli->query($sql_select_push);
				$result_select_push = $query_select_push->fetch_assoc();
				if($result_select_push){
					$pushid = $result_select_push['P_Pushid'];
					$pushpid = $result_select_push['P_Pushpid'];
					echo "<script>alert('push查询成功')</script>";
				}else{
					echo "<script>alert('pushid查询失败')</script>";
					echo $mysqli->error;
				}
			}else{
				echo "<script>alert('push更新失败')</script>";
				echo $mysqli->error;
			}
		}
		$sql_update = " UPDATE  articleinfo SET  A_Title = '$_POST[update_title]',A_KID = '$_POST[update_keyword]',A_CID =  '$_POST[update_category]',A_Desc = '$_POST[update_desc]',A_Content = '$_POST[update_content]',A_Status = '$statue',A_Adid = '$pushid',A_Padid='$pushpid' WHERE  A_AID ='$article_id'";
		$query_update = $mysqli->query($sql_update);
		if($query_update){
			echo "<script>alert('修改成功')</script>";
			echo "<meta http-equiv=\"refresh\" content=\"1; url='admin_news_mangerment.php?page=1' />";
		}else{
			echo "<script>alert('修改失败')</script>";
			echo $mysqli->error;
		}
		/*echo $sql_update;*/
/*		if($_POST['update_adid'] == 'on'){
			$sql_update = " UPDATE  articleinfo SET  A_Title = '$_POST[update_title]',A_KID = '$_POST[update_keyword]',A_CID =  '$_POST[update_category]',A_Desc = '$_POST[update_desc]',A_Content = '$_POST[update_content]',A_Status = '1',A_Adid = NULL WHERE  A_AID ='$article_id'";
		}else if($_POST['update_adid'] != 'on'){
			$sql_update = " UPDATE  articleinfo SET  A_Title = '$_POST[update_title]',A_KID = '$_POST[update_keyword]',A_CID =  '$_POST[update_category]',A_Desc = '$_POST[update_desc]',A_Content = '$_POST[update_content]',A_Status = '1',A_Adid = '$_POST[update_adid]' WHERE  A_AID ='$article_id'";
		}
		$query_update = $mysqli->query($sql_update);
		if($query_update){
			echo "<script>alert('修改成功')</script>";
		}else{
			echo "<script>alert('修改失败')</script>";
		}*/
	}
	
	if(isset($_POST['save_news'])){
		if($_POST['update_adid'] == 'on'){
			$sql_update = " UPDATE  articleinfo SET  A_Title = '$_POST[update_title]',A_KID = '$_POST[update_keyword]',A_CID =  '$_POST[update_category]',A_Desc = '$_POST[update_desc]',A_Content = '$_POST[update_content]',A_Status = '0',A_Adid = NULL WHERE  A_AID ='$article_id'";
		}else if($_POST['update_adid'] != 'on'){
			$sql_update = " UPDATE  articleinfo SET  A_Title = '$_POST[update_title]',A_KID = '$_POST[update_keyword]',A_CID =  '$_POST[update_category]',A_Desc = '$_POST[update_desc]',A_Content = '$_POST[update_content]',A_Status = '0',A_Adid = '$_POST[update_adid]' WHERE  A_AID ='$article_id'";
		}
		$query_update = $mysqli->query($sql_update);
		if($query_update){
			echo "<script>alert('修改成功')</script>";
			
		}else{
			echo "<script>alert('修改失败')</script>";
			echo $mysqli->error;
		}
	}
	

	

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="ckeditor/ckeditor.js"></script>
<title>后台首页</title>
<style>
	body{ background:#e7e7e7; margin:0; padding:0;}
	#container{ width:980px; height:auto; position:relative; margin:0 auto;}
	#container #header{ width:980px; height:90px; position:relative; background:#000; line-height:90px;color:#fff; font-size:24px; text-align:center;}
	.left{width:250px; height:auto; position:relative; float:left;}
	.left table{width:248px; height:auto; position:relative; float:left; background:#fff;}
	.left table td{ height:20px; text-align:center;}
	.left table .selected{ background:#73abe3;}
	#content{ width:730px; height:100%; position:relative; float:left; margin:2px 0px;}
	#content .title{ width:720px; height:25px; position:relative; float:left; line-height:25px; font-size:18px; color:#fff; background:#000;}
	form{margin:20px auto; width:720px; height:auto; position:relative; float:left;}
	form .inpuarea{ width:360px; height:150px;}
</style>
</head>
<body>
	<div id="container">
		<div id="header">
			818苹果站后台管理
		</div>
		<div class="left">
			<table>
				<tr><td style="background:#000000; color:#fff;">站点管理</td></tr>
				<tr><td><a href="admin_site_mangerment.php">全局管理</a></td></tr>
				<tr><td>静态页面管理</td></tr>
				<tr><td><a href="admin_link_mangerment.php?page=1">友情链接管理</a></td></tr>
				<tr><td><a href="admin_member_mangerment.php?page=1">会员管理</a></td></tr>
				<tr><td><a href="admin_tags_mangerment.php?page=1">标签管理</a></td></tr>
				<tr><td><a href="admin_search_mangerment.php">搜索管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">文章管理</td></tr>
				<tr><td><a href="admin_category_mangerment.php">文章分类管理</a></td></tr>
				<tr><td><a href="admin_news_add.php">发布文章</a></td></tr>
				<tr><td class="selected"><a href="admin_news_mangerment.php?page=1">文章管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">应用管理</td></tr>
				<tr><td><a href="admin_appcate_mangerment.php">应用分类管理</a></td></tr>
				<tr><td><a href="admin_app_add.php">添加应用</a></td></tr>
				<tr><td><a href="admin_app_mangerment.php?page=1">应用管理</a></td></tr>
				<tr><td><a href="admin_app_collection.php">应用采集</a></td></tr>
				<tr><td><a href="admin_app_collection_single.php">单应用采集</a></td></tr>
			</table>
		</div>
	<div id="content">
		<div class="title">发布文章</div>
		<form action="" method="post" enctype="multipart/form-data">
		<table>
			<tr>
				<td><label>文章标题</label></td><td><input class="input" type="text" name="update_title" size="50" value="<?php echo $result_select['A_Title']?>" />(最大长度25个字)<input type="submit" name="check_title" value="检测标题" /></td>
			</tr>
            
            <?php
				if(isset($_POST['check_title'])){
            		$sql_title = "select A_AID from articleinfo where A_Title = '$_POST[news_title]'";
					$query_title = $mysqli->query($sql_title);
					$result_title = $query_title->fetch_assoc();
					if($result_title){
			?>
						<tr><td></td><td>该标题已经存在，换一个吧</td><td></td></tr>
            <?php
					}else{
			?>
						<tr><td></td><td>该标题可用</td><td></td></tr>
            <?php
					}
				}
			?>
            </tr>
				<td><label>文章分类</label></td><td>
                <?php
                	if($result_select['A_CID'] == '1'){
				?>
                	<input type="radio" name="update_category" value="<?php echo $result_select['A_CID']?>" checked="checked" />iPhone新闻
                    <input type="radio" name="update_category" value="2" />游戏测评
                    <input type="radio" name="update_category" value="6" />娱乐八卦
                <?php
                	}
				?>
                <?php
                	if($result_select['A_CID'] == '2'){
				?>
                	<input type="radio" name="update_category" value="1"/>iPhone新闻
                    <input type="radio" name="update_category" value="<?php echo $result_select['A_CID']?>" checked="checked" />游戏测评
                    <input type="radio" name="update_category" value="6" />娱乐八卦
                <?php
                	}
				?>
                <?php
                	if($result_select['A_CID'] == '6'){
				?>
                	<input type="radio" name="update_category" value="1"/>iPhone新闻
                    <input type="radio" name="update_category" value="2" />游戏测评
                    <input type="radio" name="update_category" value="<?php echo $result_select['A_CID']?>"  checked="checked" />娱乐八卦
                <?php
                	}
				?></td>
			</tr>
           	<tr>
				<td><label>文章关键词</label></td>
                <td>
                	<input name="update_keyword" value="<?php 
										$keyword_explode = explode(",",$result_select['A_KID']);
										foreach($keyword_explode as $key=>$value){
											$value = $keyword_explode[$key];
											$sql_keyword = "select K_Name from keywordinfo where K_KID = '$value'";
											$query_keyword = $mysqli->query($sql_keyword);
											$result_keyword = $query_keyword->fetch_assoc();
											echo $result_keyword['K_Name'].",";
										}?>" />(最大长度50个字)<input type="submit" name="check_keyword" value="关键词转换" /></td>
			</tr>
            <tr>
            	<td></td><td>
                <?php
                	if(isset($_POST['check_keyword'])){
						$keyword = $_POST['update_keyword'];
						$keyword_explode = explode(",",$keyword);
						$count = count($keyword_explode);
						for($i=0;$i<$count;$i++){
						$sql = "select * from keywordinfo where K_Name = '$keyword_explode[$i]'";
						$query = $mysqli->query($sql);
						$result = $query->fetch_assoc();
						if($result){
							$new = $result['K_KID'];
							echo $result['K_KID'].",";
            			}else{
							$sql = "INSERT INTO keywordinfo (K_KID, K_ID, K_Name) VALUES (NULL, NULL, '$keyword_explode[$i]')";
							$query = $mysqli->query($sql);
				
							$sql_select = "select K_KID from keywordinfo where K_Name = '$keyword_explode[$i]'";
							$query_select = $mysqli->query($sql_select);
							$result_select = $query_select->fetch_assoc();
							echo $result_select['K_KID'].",";
						}
					}	
				}		
			?></td><td></td>            
            </tr>
			<tr>
				<td><label>文章描述</label></td><td><textarea class="inpuarea" name="update_desc"/><?php echo $result_select['A_Desc']?></textarea>(最大长度150个字)</td>
			</tr>
			<tr>
				<td><label>正文内容</label></td><td><textarea class="inpuarea" name="update_content"  class="ckeditor"><?php echo $result_select['A_Content']?></textarea>
						<script type="text/javascript">
								CKEDITOR.replace( 'update_content', {
									filebrowserBrowseUrl : 'ckfinder/ckfinder.html',
									filebrowserImageBrowseUrl : 'ckfinder/ckfinder.html?Type=Images',
									filebrowserFlashBrowseUrl : 'ckfinder/ckfinder.html?Type=Flash',
									filebrowserUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
									filebrowserImageUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
									filebrowserFlashUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
									});
                        </script>
                </td>
			</tr>
            <tr>
				<td><label>幻灯推送位置</label></td><td><input type="checkbox" name="article_overhead" value="0" />无推广<input type="checkbox" name="article_overhead" value="1" />幻灯1<input type="checkbox" name="article_overhead" value="2" />幻灯2<input type="checkbox" name="article_overhead" value="3" />幻灯3<input type="checkbox" name="article_overhead" value="4" />幻灯4<input type="checkbox" name="article_overhead" value="5" />幻灯5<input type="checkbox" name="article_overhead" value="6" />幻灯6<input type="checkbox" name="article_overhead" value="7" />幻灯7<input type="checkbox" name="article_overhead" value="8" />幻灯8<input type="checkbox" name="article_overhead" value="9" />幻灯9<input type="checkbox" name="article_overhead" value="10" />幻灯10</td>
            </tr>
			<tr>
				<td><label>右侧推送位置</label></td><td><input type="checkbox" name="article_overhead" value="19" />右侧文章1<input type="checkbox" name="article_overhead" value="20" />右侧文章2</td>
            </tr>
            <tr>
            	<td><label>配图</label></td><td><input type="file" name="uploaded" /><input type="text" name="uploaded_name" /></td>
            </tr>
			<tr>
				<td><input type="submit" name="update_news" value="正式发布" /></td>
                <td><input type="submit" name="save_news" value="保存" /></td>
			</tr>
		</table>
		</form>
	</div>
</body>
</html>